<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>宿舍数据修复工具</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .button {
            background-color: #28a745;
            color: white;
            border: none;
            padding: 12px 24px;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        .button:hover {
            background-color: #218838;
        }
        .button-secondary {
            background-color: #007bff;
        }
        .button-secondary:hover {
            background-color: #0056b3;
        }
        .logs {
            margin-top: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            height: 300px;
            overflow-y: auto;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .fix-note {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            border-radius: 4px;
            padding: 15px;
            margin-bottom: 20px;
        }
        .highlight {
            color: #dc3545;
            font-weight: bold;
        }
        .success {
            color: #28a745;
        }
        .steps {
            margin-bottom: 20px;
        }
        .step {
            margin-bottom: 10px;
            padding-left: 20px;
            position: relative;
        }
        .step:before {
            content: "✓";
            position: absolute;
            left: 0;
            color: #28a745;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <h1>宿舍数据修复工具</h1>
    <div class="container">
        <div class="fix-note">
            <h3>已修复的问题</h3>
            <p>修复了数据初始化顺序问题：<span class="highlight">现在会先创建宿舍数据，然后再为学生分配宿舍</span>，确保学生不会被分配到不存在的宿舍。</p>
        </div>
        
        <div class="steps">
            <h3>修复内容</h3>
            <div class="step">调整了初始化顺序：先创建宿舍，再分配学生</div>
            <div class="step">添加了宿舍入住人数自动更新功能</div>
            <div class="step">确保数据一致性：学生分配的宿舍一定存在</div>
        </div>
        
        <button class="button" id="fixBtn">重新初始化数据</button>
        <button class="button button-secondary" id="viewBtn">前往应用页面</button>
        <button class="button" style="background-color: #6c757d;" id="clearBtn">清除缓存数据</button>
        
        <div class="logs" id="logs"></div>
    </div>

    <script>
        const logs = document.getElementById('logs');
        const fixBtn = document.getElementById('fixBtn');
        const viewBtn = document.getElementById('viewBtn');
        const clearBtn = document.getElementById('clearBtn');
        
        function log(message) {
            const timestamp = new Date().toLocaleTimeString();
            logs.innerHTML += `[${timestamp}] ${message}\n`;
            logs.scrollTop = logs.scrollHeight;
        }
        
        function clearAllData() {
            log('正在清除所有缓存数据...');
            localStorage.removeItem('students');
            localStorage.removeItem('dormitories');
            localStorage.removeItem('initialized');
            localStorage.removeItem('classes');
            localStorage.removeItem('teachers');
            log('<span class="success">✓ 所有数据已清除</span>');
        }
        
        fixBtn.addEventListener('click', () => {
            fixBtn.disabled = true;
            log('开始重新初始化数据...');
            
            try {
                // 清除现有数据
                clearAllData();
                
                log('数据已清除，正在刷新页面以应用修复...');
                
                // 延迟刷新，让用户看到日志
                setTimeout(() => {
                    window.location.reload();
                }, 2000);
            } catch (error) {
                log(`错误: ${error.message}`);
                fixBtn.disabled = false;
            }
        });
        
        viewBtn.addEventListener('click', () => {
            window.location.href = '/';
        });
        
        clearBtn.addEventListener('click', () => {
            if (confirm('确定要清除所有缓存数据吗？')) {
                clearAllData();
            }
        });
        
        // 页面加载时显示信息
        log('欢迎使用宿舍数据修复工具');
        log('点击"重新初始化数据"按钮应用修复');
    </script>
</body>
</html>